#!/usr/bin/env bash
[[ " help letsencrypt:help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x

case "$1" in

  help | letsencrypt:help)
    help_content_func() {
      #shellcheck disable=SC2034
      declare desc="return letsencrypt plugin help content"
      cat<<help_content
    letsencrypt <app>, Enable or renew letsencrypt for app
    letsencrypt:auto-renew, Auto-renew all apps secured by letsencrypt if renewal is necessary
    letsencrypt:auto-renew <app>, Auto-renew app if renewal is necessary
    letsencrypt:cron-job [--add --remove], Add or remove a cron job that periodically calls auto-renew.
    letsencrypt:help, Display letsencrypt help
    letsencrypt:cleanup <app>, Remove stale certificate directories for app
    letsencrypt:revoke <app>, Revoke letsencrypt certificate for app
    letsencrypt:ls, List letsencrypt-secured apps with certificate expiry times
help_content
    }

    if [[ $1 = "letsencrypt:help" ]]; then
      echo -e 'Usage: dokku letsencrypt[:COMMAND]'
      echo ''
      echo 'Automatically retrieve and install Lets Encrypt certificates.'
      echo ''
      echo 'Additional commands:'
      help_content_func | sort | column -c2 -t -s,
    elif [[ $(ps -o command= $PPID) == *"--all"* ]]; then
      help_content_func
    else
      cat<<help_desc
    letsencrypt, Plugin for managing letsencrypt app integration
help_desc
    fi
    ;;

  *)
    exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
    ;;

esac
